package com.elm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.Food;
import com.elm.model.User;
import com.elm.model.UserOrder;
import com.elm.service.FoodService;
import com.elm.service.ShoppingCarService;
import com.elm.service.UserOrderService;
import com.elm.serviceimpl.FoodServiceImpl;
import com.elm.serviceimpl.ShoppingCarServiceImpl;
import com.elm.serviceimpl.UserOrderServiceImpl;
 /**
  * 
  * @author pgm
  *
  */
public class CurrentUserOrderController extends HttpServlet{
	ShoppingCarService cservice = new ShoppingCarServiceImpl();
	UserOrderService uorderservice = new UserOrderServiceImpl();
	FoodService fservice = new FoodServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<List<Integer>> ordercarids = new ArrayList<List<Integer>>();//存放满足current的carid
		List<Integer> currentcarsfoodids = new ArrayList<Integer>(); //存放满足current订单下的所有的shoppingcar中的foodid
		List<Integer> currentcarsnumfoods = new ArrayList<Integer>();//存放订单下的购物车内food的数目
		List<List<Integer>> orderallnumfood = new ArrayList<List<Integer>>();//存放所有的食物数目
		List<Double> orderprice = new ArrayList<Double>();
		List<UserOrder> pastorders = new ArrayList<UserOrder>();
		List<Food> foods = new ArrayList<Food>();
		List<List<Food>> foodss = new ArrayList<List<Food>>();
		
		req.setCharacterEncoding("utf8");
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("currentUser");
		pastorders = uorderservice.getAllPastOrder(user.getUserid(), 1);
		//所有当前订单的carid存入集合中
		for (UserOrder userOrder : pastorders) {
			userOrder.getUserorderid();
			String[] carids = userOrder.getCarid().split(",");
			List<Integer> middlefoodids = new ArrayList<Integer>();
			for (int i = 0; i < carids.length; i++) {
				Integer ints = new Integer(Integer.parseInt(carids[i]));
				middlefoodids.add(ints);
			}
			ordercarids.add(middlefoodids);
		}
		//添加食物进嵌套集合中
		for (int i = 0; i < ordercarids.size(); i++) {
			currentcarsfoodids = cservice.getShoppingCarFoodidsByCarids(ordercarids.get(i));
			currentcarsnumfoods = cservice.getShoppingCarnumfoodByCarids(ordercarids.get(i));
			foods = fservice.getFoodbyFoodids(currentcarsfoodids);
			orderallnumfood.add(currentcarsnumfoods);
			foodss.add(foods);
		}
		
		//对嵌套集合进行循环设置属性
		for (int i = 0; i < foodss.size(); i++) {
			double foodprice = 0;//存放每个订单的总价
			for(int j = 0; j < foodss.get(i).size(); j++) {
				int currentnumfood = orderallnumfood.get(i).get(j);
				foodprice = foodprice + Double.parseDouble(foodss.get(i).get(j).getFoodprice())*currentnumfood;
			}
			orderprice.add(foodprice);
		}
		session.setAttribute("currentorders", pastorders);
		req.setAttribute("orderallnumfood", orderallnumfood);
		req.setAttribute("orderprice", orderprice);
		req.setAttribute("foodsize", foodss);
		req.getRequestDispatcher("user/currentuserorder.jsp").forward(req, resp);
	}
}
